package com.moxiao.yfjh.module.blind.controller.app.controller.user;

import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.module.blind.common.BlindNotifyMessageUtils;
import com.moxiao.yfjh.module.blind.controller.admin.constellationspeech.vo.ConstellationSpeechCreateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.constellationspeech.vo.ConstellationSpeechExportReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.constellationtest.vo.*;
import com.moxiao.yfjh.module.blind.convert.constellationspeech.ConstellationSpeechConvert;
import com.moxiao.yfjh.module.blind.convert.constellationtest.ConstellationTestConvert;
import com.moxiao.yfjh.module.blind.dal.dataobject.constellationspeech.ConstellationSpeechDO;
import com.moxiao.yfjh.module.blind.dal.dataobject.constellationtest.ConstellationTestDO;
import com.moxiao.yfjh.module.blind.dal.dataobject.member.BlindMemberUserDO;
import com.moxiao.yfjh.module.blind.service.constellationspeech.ConstellationSpeechService;
import com.moxiao.yfjh.module.blind.service.constellationtest.ConstellationTestService;
import com.moxiao.yfjh.module.blind.service.member.BlindMemberUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.List;
import java.util.Objects;

import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.framework.web.core.util.WebFrameworkUtils.getLoginUserId;


@Tag(name = "App - 相亲会员星座测试")
@RestController
@RequestMapping("/blind/constellation-test")
@Validated
public class AppMemberSpeechController {

    @Resource
    private ConstellationTestService constellationTestService;
    @Resource
    private ConstellationSpeechService constellationSpeechService;
    @Resource
    private BlindMemberUserService blindMemberUserService;


    @PostMapping("/createConstellationTest")
    @Operation(summary = "创建相亲会员星座测试")
    public CommonResult<Long> createConstellationTest(@Valid @RequestBody ConstellationTestCreateReqVO createReqVO) {
        createReqVO.setUserId(getLoginUserId());
        return success(constellationTestService.createConstellationTest(createReqVO));
    }


    @GetMapping("/getConstellationTestPage")
    @Operation(summary = "获得相亲会员星座测试分页")
    public CommonResult<PageResult<ConstellationTestRespVO>> getConstellationTestPage(@Valid ConstellationTestPageReqVO pageVO) {
        PageResult<ConstellationTestDO> pageResult = constellationTestService.getConstellationTestPage(pageVO);
        PageResult<ConstellationTestRespVO> constellationTestRespVOPageResult = ConstellationTestConvert.INSTANCE.convertPage(pageResult);
        if (Objects.nonNull(constellationTestRespVOPageResult)){
            List<ConstellationTestRespVO> list = constellationTestRespVOPageResult.getList();
            if (CollectionUtils.isNotEmpty(list)){
                list.forEach(constellationTestRespVO -> {
                    //获取用户信息
                    BlindMemberUserDO memberUser = blindMemberUserService.getMemberUser(constellationTestRespVO.getUserId());
                    if (Objects.nonNull(memberUser)){
                        constellationTestRespVO.setHaveMember(memberUser.getHaveMember());
                        constellationTestRespVO.setHaveReal(memberUser.getHaveReal());
                        constellationTestRespVO.setNickname(memberUser.getNickname());
                        constellationTestRespVO.setSex(memberUser.getSex());
                        constellationTestRespVO.setAvatar(memberUser.getAvatar());
                    }
                });
            }
        }
        return success(constellationTestRespVOPageResult);
    }


    @PostMapping("/createConstellationSpeech")
    @Operation(summary = "创建相亲会员录音回复")
    public CommonResult<Boolean> createConstellationSpeech(@Valid @RequestBody List<ConstellationSpeechCreateReqVO> createReqVO) {
        //发送站内信
        if (CollectionUtils.isNotEmpty(createReqVO)){
            constellationSpeechService.createConstellation(createReqVO);
        }
        return success(true);
    }


    @GetMapping("/getConstellationTest")
    @Operation(summary = "获得相亲会员星座测试")
    public CommonResult<ConstellationTestRespVO> getConstellationTest() {
        Long loginUserId = getLoginUserId();

        ConstellationTestExportReqVO exportReqVO = new ConstellationTestExportReqVO();
        exportReqVO.setUserId(loginUserId);
        exportReqVO.setStatus((byte) 1);
        ConstellationTestDO constellationTestDO = null;
        List<ConstellationTestDO> constellationTestList = constellationTestService.getConstellationTestList(exportReqVO);
        if (CollectionUtils.isNotEmpty(constellationTestList)){
             constellationTestDO = constellationTestList.get(0);
        }
        ConstellationTestRespVO constellationTestRespVO = ConstellationTestConvert.INSTANCE.convert(constellationTestDO);
        if (Objects.nonNull(constellationTestRespVO)){
            ConstellationSpeechExportReqVO constellationSpeechExportReqVO = new ConstellationSpeechExportReqVO();
            constellationSpeechExportReqVO.setTestId(constellationTestRespVO.getId());
            List<ConstellationSpeechDO> constellationSpeechList = constellationSpeechService.getConstellationSpeechList(constellationSpeechExportReqVO);
            if (CollectionUtils.isNotEmpty(constellationSpeechList)){
                constellationTestRespVO.setSpeechRespVOList(ConstellationSpeechConvert.INSTANCE.convertList(constellationSpeechList));
            }
        }

        return success(constellationTestRespVO);
    }


}
